'use strict';

/* Controllers */
// 产品组管理
app.controller('productgroup', ['$scope', 'commonFactory', function ($scope, commonFactory) {
    	$scope.ProductList={
    		pageIndex: 1,
    		pageSize: 5,
    		ProductGroupName: '',
    		SortId: '',
    		GroupType: '',
    		CreateTime: '',
    		CreatorId: '',
    		Id: ''
    	}
        //下一页
        $scope.next = function () {
            if ($scope.page) {
                if ($scope.ProductList.pageIndex >= $scope.page) {//判断最大页码
                    $scope.ProductList.pageIndex = $scope.page;
                    return false;
                }
                $scope.ProductList.pageIndex++
                requestNoticeList();
            }
        }
        //上一页
        $scope.prev = function () {
            if ($scope.ProductList.pageIndex <= 1) {//判断最小页码
                $scope.ProductList.pageIndex = 1;
                return false;
            }
            $scope.ProductList.pageIndex--;
            requestNoticeList()
        }
    	$scope.QueryGroup = function(){
            $scope.graybtn = true;
            $scope.index = 1;
    		requestProductList();
    	}
        var requestProductList = function () {
            commonFactory.loading(true)
            commonFactory.ajax("/api/ProductGroup/GetProductGroupList", $scope.ProductList, function (result) {
                commonFactory.loading(false)
                $scope.graybtn = false;
                if (result.Code == 1) {
                    $scope.ProductGroupList = result.List;
                    $scope.ProductGroupCount = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.ProductList.pageSize);
                }
            }, function () {
                commonFactory.loading(false)
                $scope.graybtn = false;
            }, "post");
        }
        requestProductList()
}]);
// 添加产品组
app.controller('addproductgroup', ['$scope', '$http', 'commonFactory', 'toaster', function ($scope, $http, commonFactory, toaster) {
    $scope.expireTime = new Date();
    $scope.repayDate = new Date();
    $scope.allData = {
        ProductCode: "",
        ProductName: "",
        ProductGroupId: "",
        UserId: "",
        LoanPrice: "",
        ContractNo: "",
        AnnualRateOfYear: "",
        LoanPeriod: "",
        LoanPeriodUnitId: "",
        RepaymentTypeId: "",
        ProductStatus: "",
        BusinessTypeId: "",
        PublishTime: "",
        FirstOnlineTime: "",
        ExpireTime: $scope.expireTime,
        RepayDate: $scope.repayDate,
        FullTime: "",
        ProductSortId: "",
        // IsDeleted
        ShowRate: "",
        Subscript: "",
        MinInvestPrice: "",
        MaxInvestPrice: "",
        CountDown: 60,
        Description: ""
    }

    $scope.addProductGroup = function () {
        var all = $(".forminfo")
        var number = $(".forminfo[type='number']")
        for (var i = all.length - 1; i >= 0; i--) {
            if ($(all[i]).val() == "? string: ?" || $(all[i]).val() == "") {
                toaster.pop('warning', '', '数据不能为空');
                return;
            }
        }
        for (var i = number.length - 1; i >= 0; i--) {
            if ($(number[i]).val() % 1 != 0) {
                toaster.pop('warning', '', '数值必须为整数');
                return;
            }
        }
        $scope.allData.ExpireTime = commonFactory.DateFormat($scope.allData.ExpireTime)
        $scope.allData.RepayDate = commonFactory.DateFormat($scope.allData.RepayDate)
        commonFactory.loading(true)
        commonFactory.ajax("/api/product/AddLoanProduct", $scope.allData, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                toaster.pop('success', '', '添加成功');
                $timeout(function () {
                    commonFactory.go('/app/pro/product')
                }, 1000)
            } else {
                toaster.pop('error', '', '添加失败');
            }
        }, function () {

        }, "post");
    }
    $scope.mobile = "";
    $scope.getUserId = function () {
        if ($scope.mobile == "") {
            alert("查询内容不能为空");
        } else {
            commonFactory.loading(true)
            commonFactory.ajax("/api/product/ErpGetLoanPersonByMobile", { mobile: $scope.mobile }, function (result) {
                commonFactory.loading(false)
                // ?moblie=11
                if (result == null) {
                    alert("查无此人")
                    toaster.pop('warning', '', '查无此人');
                } else {
                    $scope.allData.UserId = result.Id;
                }
            }, function () {
                commonFactory.loading(false)
            }, "post");

        }
    }
}]);